package cn.innosmart.aq.view.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuntongxun.kitsdk.ECDeviceKit;

import org.json.JSONException;
import org.json.JSONObject;

import cn.innosmart.aq.R;
import cn.innosmart.aq.util.INetworkInteractUtil;
import cn.innosmart.aq.util.NetworkInteractUtil;
import cn.innosmart.aq.util.SystemConstant;
import cn.innosmart.aq.view.activity.base.BaseActivity;

/**
 * Created by David on 2015/7/28.
 */
public class ModifyNickNameActivity extends BaseActivity implements INetworkInteractUtil {
    private EditText et_nick_name;
    private NetworkInteractUtil networkInteractUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify_nickname);
        setBar();
        initView();
        setData();
        networkInteractUtil = new NetworkInteractUtil(this);
    }

    private void setData() {
        et_nick_name.setText(SystemConstant.nickname);
    }

    private void initView() {
        et_nick_name = (EditText) findViewById(R.id.et_nick_name);
    }

    private Toolbar toolbar;

    private void setBar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        ((ImageView) findViewById(R.id.iv_headicon)).setImageResource(R.drawable.toolbar_back);
        toolbar.setTitle("");
        ((TextView) findViewById(R.id.tv_toolbar)).setText(getString(R.string.activity_modify_nick_name));
        setSupportActionBar(toolbar);
        toolbar.setOnMenuItemClickListener(onMenuItemClick);
        findViewById(R.id.iv_headicon).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                mDrawer.openDrawer(GravityCompat.START);
                mHandler.sendEmptyMessage(EXIT);
            }
        });
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.action_sure:
                    mHandler.sendEmptyMessage(SURE);
                    break;
            }
            return true;
        }
    };

    private final int EXIT = 0;
    private final int SURE = 1;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case EXIT:
                    exit();
                    break;
                case SURE:
                    String newName = et_nick_name.getText().toString();
                    if (TextUtils.isEmpty(newName)) {
                        showToast(getString(R.string.nickname_can_not_empty));
                        break;
                    }
                    if (newName.equals(SystemConstant.nickname)) {
                        exit();
                        break;
                    }
                    JSONObject jsParam = new JSONObject();
                    try {
                        jsParam.put("nickname", newName);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    networkInteractUtil.syncModifyNickName(jsParam);
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        setBaseDialogContext(this);
        super.onResume();
        toolbar.setNavigationIcon(null);
    }

    @Override
    public void onBackPressed() {
        exit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        getMenuInflater().inflate(R.menu.menu_toolbar_modify_sure, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public void onNetworkInteractAllOver(int ret) {

    }

    @Override
    public void onNetworkInteractOver(int ret, String flag, Object object) {
        if (flag.equals(networkInteractUtil.MODIFYNICKNAME)) {
            if (ret == 0) {
                showToast(getString(R.string.modify_success));
                SystemConstant.nickname = et_nick_name.getText().toString();
                ECDeviceKit.getInstance().setNickName(SystemConstant.nickname);
                exit();
            } else if (ret == -1) {
                showToast(getString(R.string.modify_failed));
            }
        }
    }
}
